package com.wiwj.es;

import java.net.InetAddress;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.get.GetField;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import com.alibaba.fastjson.JSONObject;


public class ESUtils {
	
	 /**
     * 组织json串, 方式1,直接拼接
     */
    public String createJson1() {
        String json = "{" +
                "\"user\":\"kimchy\"," +
                "\"postDate\":\"2013-01-30\"," +
                "\"message\":\"trying out Elasticsearch\"" +
            "}";
        return json;
    }
    
    /**
     * 使用map创建json
     */
    public Map<String, Object> createJson2() {
        Map<String,Object> json = new HashMap<String, Object>();
        json.put("user", "kimchy");
        json.put("postDate", new Date());
        json.put("message", "trying out elasticsearch");
        return json;
    }
    
    /**
     * 使用fastjson创建
     */
    public JSONObject createJson3() {
        JSONObject json = new JSONObject();
        json.put("user", "kimchy");
        json.put("postDate", new Date());
        json.put("message", "trying out elasticsearch");
        return json;
    }
    
    /**
     * 使用es的帮助类
     */
    public XContentBuilder createJson4() throws Exception {
        // 创建json对象, 其中一个创建json的方式
        XContentBuilder source = XContentFactory.jsonBuilder()
            .startObject()
                .field("user", "kimchy")
                .field("postDate", new Date())
                .field("message", "trying to out ElasticSearch")
            .endObject();
        return source;
    }

	public static void main(String[] args) throws Exception{
		Map<String,String> map = new HashMap<String, String>();
		map.put("cluster.name", "my-5i5j");
		Settings settings = Settings.builder().put(map).build();
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.1.66.76"),9300));
		
		/*
		//查看集群信息
		List<DiscoveryNode> nodes = client.connectedNodes();
		for(DiscoveryNode node : nodes){
			System.out.println(node.getHostAddress());
			System.out.println(node.getVersion());
		}
		*/
		
		/**
	     * 存入索引中
	     * @throws Exception
	     */
		/*
		ESUtils utils = new ESUtils();
		XContentBuilder source = utils.createJson4();
        // 存json入索引中
        IndexResponse response = client.prepareIndex("twitter", "tweet", "1").setSource(source).get();
        // 结果获取
        String index = response.getIndex();
        String type = response.getType();
        String id = response.getId();
        long version = response.getVersion();
        System.out.println(index + " : " + type + ": " + id + ": " + version);
		*/
		
		/**
		 * get API 获取指定文档信息
		 */
		/*
		GetResponse response = client.prepareGet("twitter", "tweet", "1").setOperationThreaded(false).get();
		System.out.println(response.getSourceAsString());
		*/
		/*
		Map<String,Object> mps = response.getSource();
		for(String key : mps.keySet()){
			System.out.println(key + " : " + mps.get(key));
		}
		*/
		
		
		/**
		 * 删除 delete api
		 */
		/*
		DeleteResponse deleteResponse = client.prepareDelete("twitter", "tweet", "1").get();
		String index = deleteResponse.getIndex();
		String type = deleteResponse.getType();
		String id = deleteResponse.getId();
		long version = deleteResponse.getVersion();
		System.out.println(index + " : " + type + ": " + id + ": " + version);
		*/
	    
		/**
		 * 测试更新 update API
		 * 使用UpdateRequest
		 */
		/*
		UpdateRequest updateRequest = new UpdateRequest();
		updateRequest.index("twitter");
		updateRequest.type("tweet");
		updateRequest.id("1");
		updateRequest.doc(XContentFactory.jsonBuilder().startObject().field("gender", "male").field("message", "hello").endObject());
		UpdateResponse response = client.update(updateRequest).get();
		// 打印
        String index = response.getIndex();
        String type = response.getType();
        String id = response.getId();
        long version = response.getVersion();
        System.out.println(index + " : " + type + ": " + id + ": " + version);
        
        GetResponse getResponse = client.prepareGet("twitter", "tweet", "1").setOperationThreaded(false).get();
        System.out.println(getResponse.getSourceAsString());
        */
		
	}

}
